package v1

import (
	"app/api/v1/response"
	"app/internal/app/middleware"
	"app/internal/app/model"
	"app/internal/pkg/ecode"
	"fmt"

	"github.com/gin-gonic/gin"
)

type UserVO struct {
	ID        uint   `json:"id"`
	Account   uint   `json:"account"`
	Name      string `json:"name"`
	Nickname  string `json:"nickname"`
	Telephone string `json:"telephone"`
}

func ToUserVO(user *model.User) UserVO {
	return UserVO{
		ID:        user.ID,
		Account:   user.Account,
		Name:      user.Name,
		Nickname:  user.Nickname.String,
		Telephone: user.Telephone.String,
	}
}

func GetUser(ctx *gin.Context) {
	token, err := ctx.Cookie("token")
	if err != nil {
		response.Fail(ctx, ecode.ERROR_TOKEN_NOT_EXIST)
		return
	}

	claims, err := middleware.JWT.ParserToken(token)
	if err != nil {
		response.Fail(ctx, err)
		return
	}

	user, err := model.GetUserByID(claims.ID)
	if err != nil {
		response.Fail(ctx, fmt.Errorf("GetUserByID: %w", err))
		return
	}

	response.Success(ctx, "", ToUserVO(user))
}
